<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    show-footer
    width="50%"
    @ok="handleSubmit"
  >
    <template #title>
      {{ isUpdate ? '编辑' : '新增' }}
    </template>
    <template #footer>
      <a-button @click="closeModal" :loading="btnLoading">取消</a-button>
      <a-button
        type="primary"
        @click="handleSubmit(false)"
        :loading="btnLoading"
      >保存</a-button
      >
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, useForm } from '@/components/Form/index';
import { formSchema } from './data';
import { useMessage } from '@/hooks/web/useMessage';
import { useThrottleFn } from '@vueuse/core';
import { defHttp } from '@/utils/http/axios';

const emit = defineEmits(['success', 'register']);
let isUpdate = ref(true);
const btnLoading = ref(true);
const { createMessage } = useMessage();
const [
  registerForm,
  { resetFields, setFieldsValue, validate },
] = useForm({
  labelWidth: 120,
  schemas: [
    // 标题 展示图 位置
    {
      field: 'title',
      component: 'Input',
      label: '文章标题',
      componentProps: {
        placeholder: '请输入文章标题',
      },
      required: true,
    },
    {
      field: 'id',
      component: 'Input',
      label: 'id',
      show: false,
    },
    {
      field: 'subTitle',
      component: 'Input',
      label: '简略标题',
      componentProps: {
        placeholder: '请输入简略标题',
      },
      required: true,
    },
    {
      field: 'articleCategoryId',
      label: '分类栏目',
      component: 'ApiSelect',
      componentProps: {
        api: '/article/article/categoryList',
        treeDefaultExpandAll: false,
        fieldNames: {
          label: 'name',
          key: 'id',
          value: 'id',
        },
      },
      required: false,
    },
    {
      field: 'author',
      component: 'Input',
      label: '文章作者',
      componentProps: {
        placeholder: '请输入文章作者',
      },
      required: true,
    },
    {
      field: 'source',
      component: 'Input',
      label: '文章来源',
      componentProps: {
        placeholder: '请输入文章来源',
      },
      required: true,
    },
    {
      field: 'sort',
      component: 'InputNumber',
      label: '排序',
      componentProps: {
        min: 0,
        max: 1000000,
        step: 1,
      },
      required: true,
    },
    {
      field: 'isTop',
      component: 'RadioGroup',
      label: '是否置顶',
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      required: false,
    },
    {
      field: 'isIndex',
      component: 'RadioGroup',
      label: 'app首页是否显示',
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      required: false,
    },
    {
      field: 'content',
      component: 'RichText',
      label: '内容',
    },
    {
      field: 'showImageUrl',
      component: 'AvUpload',
      label: '展示图',
      required: false,
    },
  ],
  showActionButtonGroup: false,
  baseColProps: { span: 24 },
  mode: 'edit',
});
// 文档名称
const adName = ref();
const isEdit = ref(false);
const [registerModal, { closeModal, changeLoading }] =
  useModalInner(async (data) => {
    resetFields();
    isEdit.value = false;
    changeLoading(true);
    btnLoading.value = true;
    isUpdate.value =!!data?.isUpdate;
    if (isUpdate.value) {
      getFormData(data.record.id);
      setFieldsValue({ ...data.record });
    }
    changeLoading(false);
    btnLoading.value = false;
  },
);
const handleSubmit = useThrottleFn(doSubmit, 1000);
async function doSubmit(hasNext: boolean) {
  try {
    changeLoading(true);
    btnLoading.value = true;
    const values = await validate();
    if (isUpdate.value) {
      const res = await defHttp.post({ url: '/article/article/edit', data: values });
      createMessage.success(res);
    } else {
      const res = await defHttp.post({ url: '/article/article/add', data: values });
      createMessage.success(res);
    }
    emit('success');
    closeModal();
  } finally {
    btnLoading.value = false;
    changeLoading(false);
  }
}
async function getFormData(id) {
  debugger;
  const res = await defHttp.get({
    url: '/article/article/toEdit?id=' + id,
  });
  if (res) {
    debugger;
    await setFieldsValue({
      ...res,
    });
    changeLoading(false);
  }
}
async function handleStopStart(param) {
  const res = await defHttp.post({
    url: `/article/article/articleStatus`,
    data: { aid: param },
  });
  //reload();
}
</script>
